Skip to content

fix(services): batch the per-repo outcome-patterns snapshot read - #10165

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/repo-outcome-patterns-batch-read-10024
Jul 31, 2026
Merged

fix(services): batch the per-repo outcome-patterns snapshot read#10165
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/repo-outcome-patterns-batch-read-10024

Conversation

@shin-core

Copy link
Copy Markdown
Contributor

What & why

loadRepoOutcomePatternsMap fired one listSignalSnapshots query per registered repo, concurrently and unbatched, and discarded 99 of the up-to-100 rows each one returned (each carrying a full payload_json) to use exactly one:

repositories.filter((r) => r.isRegistered).map(async (repo) => {
  const latest = (await listSignalSnapshots(env, REPO_OUTCOME_PATTERNS_SIGNAL, repo.fullName))[0];
  if (latest) map.set(repo.fullName.toLowerCase(), latest.payload );
});

listSignalSnapshots has no limit parameter and hard-caps at 100 rows including each payload_json. This runs on the contributor decision-pack build path, so the read scaled linearly in DB round trips with the installed-repo count — the exact shape listRecentSignalSnapshotsForTargets was written to fix (it selects payload_json, batches at SIGNAL_SNAPSHOT_TARGET_KEY_SQL_BATCH = 90 keys per round trip, and takes an explicit maxPerTarget; repo-doc-refresh-runner.ts already uses the sibling bulk helper).

The fix

Read every registered repo's latest snapshot in one bulk calllistRecentSignalSnapshotsForTargets(env, REPO_OUTCOME_PATTERNS_SIGNAL, fullNames, 1) — replacing the per-repo Promise.all loop. The helper keys by the exact targetKey string, so the caller lowercases on the way out to preserve the map's existing lowercased-key contract (decision-pack.ts's lookups). listRecentSignalSnapshotsForTargets (not the Latest variant) is the one that selects payload_json, and maxPerTarget: 1 takes only the newest snapshot per repo.

Unchanged: the isRegistered filter, a registered repo with no snapshot still absent from the map, the single-repo loadOrComputeRepoOutcomePatternsResponse path, computeRepoOutcomePatterns, and listSignalSnapshots' signature and hard limit(100) (other callers depend on it).

Tests (test/unit/repo-outcome-patterns-service.test.ts)

  • Three registered + one unregistered repo → the map has exactly the three lowercased registered keys with their payloads; the unregistered repo is absent.
  • The DB.prepare invocation count does not grow with the repo count — 3 repos and 12 repos produce the same number of prepared statements (one batch, 12 < 90). Fails on main (linear per-repo loop).
  • REGRESSION: a stored targetKey whose casing differs from the requested fullName still resolves to a lowercased map key, so the helper's exact-casing contract doesn't silently drop it.
  • No registered repos → empty map (the no-bulk-read early path).
  • The existing loadRepoOutcomePatternsMap test is preserved.

Validation

  • Diff coverage on src/services/repo-outcome-patterns.ts is 100% line and branch (the registered/unregistered continue, the snapshot present/absent arms, and the empty-list path).
  • npm run typecheck clean; npm run engine-parity:drift-check passes (not a twin); npm run dead-exports:check clean; the suite (14 tests) green.
  • git diff --check clean; no schema/migration/generated-artifact change.

Closes #10024

loadRepoOutcomePatternsMap fired one listSignalSnapshots query per registered repo,
concurrently and unbatched, and discarded 99 of the up-to-100 full payloads each one
returned to use exactly one — scaling DB round trips linearly with the installed-repo
count on the contributor decision-pack build path. The bulk helper for exactly this
shape already exists: listRecentSignalSnapshotsForTargets selects payload_json, batches
at SIGNAL_SNAPSHOT_TARGET_KEY_SQL_BATCH keys per round trip, and takes an explicit
maxPerTarget.

Read every registered repo's latest snapshot in one bulk call
(listRecentSignalSnapshotsForTargets(env, SIGNAL, fullNames, 1)) instead of the per-repo
Promise.all loop, lowercasing the returned keys on the way out to preserve the map's
existing lowercased-key contract (the helper keys by the exact targetKey string). The
isRegistered filter, the single-repo loadOrComputeRepoOutcomePatternsResponse path,
computeRepoOutcomePatterns, and listSignalSnapshots' signature are all unchanged; a
registered repo with no snapshot is still absent from the map.

Closes JSONbored#10024
@shin-core
shin-core requested a review from JSONbored as a code owner July 31, 2026 10:27
@loopover-orb

loopover-orb Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-31 10:42:30 UTC

2 files · 1 AI reviewer · no blockers · readiness 98/100 · CI green · clean

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
Replaces a per-registered-repo Promise.all fan-out (each firing its own listSignalSnapshots query with a 100-row hard cap and full payload_json) with a single batched listRecentSignalSnapshotsForTargets(env, signal, fullNames, 1) call, then maps the returned targetKey→snapshots back onto the existing lowercased-key map contract. The lowercasing-on-the-way-out logic correctly preserves the existing map key contract, and the filter/skip logic for unregistered repos and repos with no snapshot is unchanged. Tests cover the empty-map case, casing mismatch, and a prepare-count invariance check across repo counts, giving reasonable confidence the bulk path behaves as claimed.

Nits — 4 non-blocking
  • The prepare-count test only checks 3 vs 12 repos, both under the SIGNAL_SNAPSHOT_TARGET_KEY_SQL_BATCH=90 threshold, so it doesn't actually exercise or prove correctness of the multi-batch code path for >90 registered repos.
  • src/services/repo-outcome-patterns.ts: the loop still allocates a full intermediate fullNames array before the bulk call; trivial but could dedupe if duplicate fullNames are ever possible in the input.
  • Add a test with repoCount > 90 to confirm the batching logic in listRecentSignalSnapshotsForTargets is actually exercised across multiple round trips, not just proven equal below the threshold.
  • Consider asserting in a test that byTargetKey.get(repo.fullName) returns snapshots ordered newest-first when multiple snapshots exist per target, since maxPerTarget:1 relies on that ordering.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #10024
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 78 registered-repo PR(s), 60 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor shin-core; Gittensor profile; 78 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: significant
Linked issue satisfaction

Addressed
The diff replaces the per-repo listSignalSnapshots loop with a single listRecentSignalSnapshotsForTargets(env, REPO_OUTCOME_PATTERNS_SIGNAL, fullNames, 1) call, preserves the isRegistered filter and lowercased-key contract, and leaves listSignalSnapshots, listRecentSignalSnapshotsForTargets, loadOrComputeRepoOutcomePatternsResponse, and computeRepoOutcomePatterns unchanged as required.

Review context
  • Author: shin-core
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: not available
  • Official Gittensor activity: 78 PR(s), 0 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Keep the PR focused and include validation evidence before maintainer review.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.43%. Comparing base (55cbeb9) to head (cbeca6d).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10165   +/-   ##
=======================================
  Coverage   80.43%   80.43%           
=======================================
  Files         282      283    +1     
  Lines       58744    58770   +26     
  Branches     6963     6971    +8     
=======================================
+ Hits        47248    47274   +26     
  Misses      11205    11205           
  Partials      291      291           
Flag Coverage Δ
backend 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/services/repo-outcome-patterns.ts 100.00% <100.00%> (ø)

@loopover-orb loopover-orb Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit 0a67db2 into JSONbored:main Jul 31, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

services(repo-outcome-patterns): batch the per-repo snapshot read the bulk helper already exists for

1 participant